<!DOCTYPE html>
<html lang="en">
<head>
		<title>Snippets &mdash; Articles tagged python</title>
		<meta charset="utf-8" />
		<link rel="profile" href="http://gmpg.org/xfn/11" />
		<link rel="stylesheet" type="text/css" href="https://corteousquestions.club/theme/css/style.css" />
		<link rel='stylesheet' id='oswald-css'  href='http://fonts.googleapis.com/css?family=Oswald&#038;ver=3.3.2' type='text/css' media='all' />
		<style type="text/css">
			body.custom-background { background-color: #f5f5f5; }
		</style>
		<link rel="alternate" type="application/atom+xml"
			title="Snippets — Flux Atom"
			href="https://corteousquestions.club/" /> 
		<!--[if lte IE 8]><script src="https://corteousquestions.club/theme/js/html5shiv.js"></script><![endif]-->
</head>

<body class="home blog custom-background " >
	<div id="container">
		<div id="header">
				<h1 id="site-title"><a href="https://corteousquestions.club">Snippets</a></h1>
<h2 id="site-description">All that I will forget is here</h2>		</div><!-- /#banner -->
		
		<div id="menu">
			<div class="menu-navigation-container">
				<ul id="menu-navigation" class="menu">
						<li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="https://corteousquestions.club/category/misc.html">misc</a></li>

				</ul>
			</div> <!--/#menu-navigation-container-->
		</div><!-- /#menu -->
		
		<div class="page-title">
<div class="page-title">
	<h2>Tagged with <span>python</span> &hellip;</h2>
</div>
		</div>
	
		<div id="contents">
<div class="post type-post status-publish format-standard hentry category-general" id="post">
	<div class="entry-meta">
		<div class="date"><a href="https://corteousquestions.club/read-python-file-to-list.html">Mon 19 April 2021</a></div>
		<span class="byline">By <a href="https://corteousquestions.club/author/mclds.html">mclds</a></span>
			<span class="cat-links"><a href="https://corteousquestions.club/category/misc.html" title="View all posts in misc" rel="category tag">misc</a></span>
	</div> <!-- /#entry-meta -->
	<div class="main">
		<h2 class="entry-title">
			<a href="https://corteousquestions.club/read-python-file-to-list.html" title="Permalink to How to read a file into a list" rel="bookmark">How to read a file into a list</a>
		</h2>
		<div class="entry-content">
			<hr>
<p>with open('C:/path/numbers.txt') as f:
     lines = f.read().splitlines()</p>
<hr>
<p>this will give you a list of values (strings)
 you had in your file, with newlines stripped.
 https://bit.ly/38TxQ4y</p>
		</div> <!--/#entry-content-->
    		<span class="tag-links"><strong>Tagged</strong>
 <a href="https://corteousquestions.club/tag/file.html" rel="tag">file</a>,  <a href="https://corteousquestions.club/tag/list.html" rel="tag">list</a>,  <a href="https://corteousquestions.club/tag/python.html" rel="tag">python</a>    		</span>
	</div> <!--/#main-->
</div>  <!--/#post--><div class="post type-post status-publish format-standard hentry category-general" id="post">
	<div class="entry-meta">
		<div class="date"><a href="https://corteousquestions.club/python-packages-installed-from-source.html">Sat 17 April 2021</a></div>
		<span class="byline">By <a href="https://corteousquestions.club/author/mclds.html">mclds</a></span>
			<span class="cat-links"><a href="https://corteousquestions.club/category/misc.html" title="View all posts in misc" rel="category tag">misc</a></span>
	</div> <!-- /#entry-meta -->
	<div class="main">
		<h2 class="entry-title">
			<a href="https://corteousquestions.club/python-packages-installed-from-source.html" title="Permalink to Install Python package from source" rel="bookmark">Install Python package from source</a>
		</h2>
		<div class="entry-content">
			<p>sudo python setup.py install</p>
		</div> <!--/#entry-content-->
    		<span class="tag-links"><strong>Tagged</strong>
 <a href="https://corteousquestions.club/tag/package.html" rel="tag">package</a>,  <a href="https://corteousquestions.club/tag/python.html" rel="tag">python</a>,  <a href="https://corteousquestions.club/tag/setuppy.html" rel="tag">setup.py</a>    		</span>
	</div> <!--/#main-->
</div>  <!--/#post--><div class="post type-post status-publish format-standard hentry category-general" id="post">
	<div class="entry-meta">
		<div class="date"><a href="https://corteousquestions.club/create-dictionary-multiple-values.html">Sun 21 April 2019</a></div>
		<span class="byline">By <a href="https://corteousquestions.club/author/mclds.html">mclds</a></span>
			<span class="cat-links"><a href="https://corteousquestions.club/category/misc.html" title="View all posts in misc" rel="category tag">misc</a></span>
	</div> <!-- /#entry-meta -->
	<div class="main">
		<h2 class="entry-title">
			<a href="https://corteousquestions.club/create-dictionary-multiple-values.html" title="Permalink to Create dictionary with multiple values" rel="bookmark">Create dictionary with multiple values</a>
		</h2>
		<div class="entry-content">
			<p>There are a couple of ways to add values to key, and to create a list
 if one isn't already there. I'll show one such method in little steps.</p>
<hr>
<p>key = somekey
 a.setdefault(key, [])
 a[key].append(1)</p>
<hr>
<p>Results:</p>
<hr>
<blockquote>
<blockquote>
<blockquote>
<p>a
 {'somekey': [1]}
 Next, try:
 key ='somekey'
 a.setdefault(key, [])
 a[key].append(2)</p>
</blockquote>
</blockquote>
</blockquote>
<hr>
<p>Results:</p>
<hr>
<blockquote>
<blockquote>
<blockquote>
<p 2_="2]" _1_="[1," _somekey_:="'somekey':">a</p>
</blockquote>
</blockquote>
</blockquote>
<hr>
<p>The magic of setdefault is that it initializes the value for that key
 if that key is not defined, otherwise it does nothing. Now, noting
 that setdefault returns the key you can combine these into a single line:</p>
<hr>
<p>a.setdefault('somekey',[]).append('bob')</p>
<hr>
<p>Results:</p>
<hr>
<blockquote>
<blockquote>
<blockquote>
<p 2_="2," _1_="[1," _bob_="'bob']" _somekey_:="'somekey':">a</p>
</blockquote>
</blockquote>
</blockquote>
<hr>
<p>https://bit.ly/3p3h2O4</p>
		</div> <!--/#entry-content-->
    		<span class="tag-links"><strong>Tagged</strong>
 <a href="https://corteousquestions.club/tag/dictionary.html" rel="tag">dictionary</a>,  <a href="https://corteousquestions.club/tag/key.html" rel="tag">key</a>,  <a href="https://corteousquestions.club/tag/python.html" rel="tag">python</a>    		</span>
	</div> <!--/#main-->
</div>  <!--/#post--><div class="post type-post status-publish format-standard hentry category-general" id="post">
	<div class="entry-meta">
		<div class="date"><a href="https://corteousquestions.club/print-file-content.html">Sun 21 April 2019</a></div>
		<span class="byline">By <a href="https://corteousquestions.club/author/mclds.html">mclds</a></span>
			<span class="cat-links"><a href="https://corteousquestions.club/category/misc.html" title="View all posts in misc" rel="category tag">misc</a></span>
	</div> <!-- /#entry-meta -->
	<div class="main">
		<h2 class="entry-title">
			<a href="https://corteousquestions.club/print-file-content.html" title="Permalink to Print file content" rel="bookmark">Print file content</a>
		</h2>
		<div class="entry-content">
			<p>Opening a file in python for reading is easy:
 with open('example.txt', 'r') as f:
 To get everything in the file, just use read()
 file_contents = f.read()
 And to print the contents, just do:
 print (file_contents)
 https://bit.ly/3io5Ucd</p>
		</div> <!--/#entry-content-->
    		<span class="tag-links"><strong>Tagged</strong>
 <a href="https://corteousquestions.club/tag/file.html" rel="tag">file</a>,  <a href="https://corteousquestions.club/tag/print.html" rel="tag">print</a>,  <a href="https://corteousquestions.club/tag/python.html" rel="tag">python</a>    		</span>
	</div> <!--/#main-->
</div>  <!--/#post--><div class="post type-post status-publish format-standard hentry category-general" id="post">
	<div class="entry-meta">
		<div class="date"><a href="https://corteousquestions.club/delete-lines-from-file-python.html">Sun 21 April 2019</a></div>
		<span class="byline">By <a href="https://corteousquestions.club/author/mclds.html">mclds</a></span>
			<span class="cat-links"><a href="https://corteousquestions.club/category/misc.html" title="View all posts in misc" rel="category tag">misc</a></span>
	</div> <!-- /#entry-meta -->
	<div class="main">
		<h2 class="entry-title">
			<a href="https://corteousquestions.club/delete-lines-from-file-python.html" title="Permalink to Delete lines from file" rel="bookmark">Delete lines from file</a>
		</h2>
		<div class="entry-content">
			<p>You need to open the file and read its contents in memory,
 then open the file again write the line to it but without
 the line you wish to omit
 Example:</p>
<hr>
<p>with open('yourfile.txt', 'r') as f:
     lines = f.readlines()
 with open('yourfile.txt', 'w') as f:
     for line in lines:
             if line.strip('
') != 'nickname_to_delete':
                 f.write(line)'</p>
<hr>
<p>https://bit.ly/3nUlzRp</p>
		</div> <!--/#entry-content-->
    		<span class="tag-links"><strong>Tagged</strong>
 <a href="https://corteousquestions.club/tag/delete.html" rel="tag">delete</a>,  <a href="https://corteousquestions.club/tag/file.html" rel="tag">file</a>,  <a href="https://corteousquestions.club/tag/python.html" rel="tag">python</a>    		</span>
	</div> <!--/#main-->
</div>  <!--/#post-->		</div>
		
		<div id="footer">
			<p>Powered by <a href="http://getpelican.com">Pelican</a>, theme by <a href="http://bunnyman.info">tBunnyMan</a>.</p>
		</div><!-- /#footer -->
	</div><!-- /#container -->
	<div style="display:none"></div>
</body>
</html>